package com.ihighest.app.infox.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.ihighest.app.infox.R;
import com.ihighest.app.infox.activity.LableBindingActivity;
import com.ihighest.app.infox.myview.Tag;
import com.ihighest.app.infox.myview.TagListView;
import com.ihighest.app.infox.myview.TagListView.OnTagClickListener;
import com.ihighest.app.infox.myview.TagView;

public class SelectLableAdapter extends BaseAdapter {
	Context context;
	JSONArray jsonArray;
	LayoutInflater inflater;
	private JSONArray array;
	private List<Tag> mTags;
	public List<Tag> tags;

	// 当前已选标签数
	private int num = 0;
	private int max;

	// private String[] titles = { "安全必备", "360手机卫士", "QQ", "输入法", "微信", "蘑菇街"
	// };
	HashMap<Integer, Boolean> state = new HashMap<Integer, Boolean>();

	public SelectLableAdapter(Context context) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
	}

	public SelectLableAdapter(Context context, JSONArray jsonArray,
			List<Tag> tags) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.array = jsonArray;
		this.tags = tags;
	}

	public List<Tag> getTags() {
		return tags;
	}

	@Override
	public int getCount() {
		return array == null ? 0 : array.length();
		// return 5;
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View contextView, ViewGroup parent) {
		ViewHolder holder;
		if (contextView == null) {
			holder = new ViewHolder();
			contextView = inflater.inflate(
					R.layout.infox_activity_lablebinding_item, null);
			holder.lableType = (TextView) contextView
					.findViewById(R.id.lableType);
			holder.selectCount = (TextView) contextView
					.findViewById(R.id.selectCount);
			holder.mTagListView = (TagListView) contextView
					.findViewById(R.id.mTagListView);
			contextView.setTag(holder);

		} else {
			holder = (ViewHolder) contextView.getTag();
		}
		try {
			holder.lableType.setText(array.getJSONObject(position).getString(
					"title"));
			setUpData(array.getJSONObject(position).getJSONArray("sub"));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		holder.mTagListView.setTags(mTags);
		for (int i = 0; i < tags.size(); i++) {
			for (int j = 0; j < mTags.size(); j++) {
				if (mTags.get(j).getId().equals(tags.get(i).getId())) {
					TagView inflate = (TagView) holder.mTagListView
							.getViewByTag(mTags.get(j));
					inflate.setTextColor(context.getResources().getColor(
							R.color.orange_red));
					inflate.setChecked(true);
				}
			}

		}
		holder.mTagListView.setOnTagClickListener(new OnTagClickListener() {

			@SuppressLint("ShowToast")
			@Override
			public void onTagClick(TagView tagView, Tag tag) {
				if (tagView.isChecked()) {
					state.put(position, true);
					// SharedPreferencesKit.getJsonObject(context,
					// Const.SP_KEY_CONFIG_JSON).getInt("member_tag_bind_max_limit")
					if (num < 5) {
						num++;
						LableBindingActivity.a++;
						tagView.setTextColor(context.getResources().getColor(
								R.color.orange_red));
						tags.add(tag);

						// setLableBind(tag.getId(), "1");
					} else {

						Toast.makeText(context, "最多选5个标签", 0).show();
						tagView.setChecked(false);
						return;
					}

				} else {
					state.remove(position);
					num = num - 1;
					LableBindingActivity.a--;
					tagView.setTextColor(context.getResources().getColor(
							R.color.deepBlackTextcolor));
					// setLableBind(tag.getId(), "2");
					tags.remove(tag);
				}
			}
		});
		return contextView;
	}

	private void setUpData(JSONArray mArray) {
		mTags = new ArrayList<>();
		for (int i = 0; i < mArray.length(); i++) {
			Tag tag = new Tag();

			try {
				tag.setId(mArray.getJSONObject(i).getString("id"));
				tag.setTitle(mArray.getJSONObject(i).getString("title"));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			mTags.add(tag);
		}
	}

	class ViewHolder {
		TextView lableType;
		TextView selectCount;
		TagListView mTagListView;
	}

}
